#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import MySQLdb

DATABASE_NAME = 'pmc_meta'
HOST = '10.10.31.17'
USER_NAME = 'pmc'
PASSWORD = 'pmc'
conn = MySQLdb.connect(host=HOST, user=USER_NAME, passwd=PASSWORD, db=DATABASE_NAME, charset="utf8")

infile=open("D:/data/if/if.txt","r")
infile.readline()
infile.readline()

count=0
for line in infile:
    line=line.rstrip("\n")
    splitline=line.split("\t")
    issn=splitline[1]
    if issn!="":
        cursor = conn.cursor()
        sql = "SELECT id FROM tb_dds_journal WHERE issn_print=%s OR issn_electronic=%s"
        params=(issn,issn)
        cursor.execute(sql,params)
        result = cursor.fetchall()
        if len(result)!=0:
            for item in result:
                for i in xrange(2,len(splitline)):
                    if splitline[i]!="":
                        year=str(i+1990)
                        cursor1=conn.cursor()
                        count+=1
                        sql1="INSERT INTO tb_dds_if_year VALUES(0,%s,%s,%s)"
                        print count
                        params1=(str(item[0]),year,splitline[i])
                        cursor1.execute(sql1,params1)
                        conn.commit()
                        cursor1.close()
        cursor.close()

infile.close()

conn.close()

